package com.ming.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.common.Result;
import com.ming.domain.SysUser;
import com.ming.from.SysPermissionForm;
import com.ming.query.SysPermissionQuery;
import com.ming.vo.SysPermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.mapper.SysPermissionMapper;
import com.ming.domain.SysPermission;
import com.ming.service.SysPermissionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-04-09 23:15
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService{

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    /**
     * 查询所有的权限 用于左侧菜单的显示
     *
     * @return
     */
    @Override
    public Result queryList() {
        List<SysPermissionVO> data =  sysPermissionMapper.selectAll();
        return new Result(data);
    }

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result doPage(SysPermissionQuery query) {
        Page<SysPermission> page = new Page<>(query.getPage(),query.getLimit());
        Page<SysPermission> permissionPage = sysPermissionMapper.selectPage(page, new LambdaQueryWrapper<SysPermission>()
                .like(!ObjectUtils.isEmpty(query.getTitle()),SysPermission::getTitle,query.getTitle())
        );
        sysPermissionMapper.selectList(query);
        return new Result(page);
    }

    /**
     * 新增
     *
     * @param form
     * @return
     */
    @Override
    public Result add(SysPermissionForm form) {
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(form,sysPermission);
        sysPermissionMapper.insert(sysPermission);
        return new Result();
    }

    /**
     * 修改
     *
     * @param form
     * @return
     */
    @Override
    public Result update(SysPermissionForm form) {
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(form,sysPermission);
        sysPermissionMapper.updateById(sysPermission);
        return new Result();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(Integer id) {
        //需要删除的权限的id
        List<Integer> parentId = new ArrayList<>();
        parentId.add(id);
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        //权限一旦删除，角色权限关系也要删除
        sysPermissionMapper.deleteRelByPermissionId(id);
        //删除具体的权限 因为当前权限有可能是父权限 也有可能是子权限 及 子子孙孙
        //所以先将子孙权限查出来
        while(true) {
            ids = sysPermissionMapper.selectAllByParentId(ids);
            if (ids == null || ids.isEmpty()) {
                //跳出
                break;
            }
            parentId.addAll(ids);
        }
        //根据id 删除权限
        sysPermissionMapper.deleteById(parentId);
        return new Result();
    }
}
